//
//  SendCommentController.m
//  suRead
//
//  Created by nancy  on 16/9/20.
//  Copyright © 2016年 bao. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "SendCommentController.h"
#import "Masonry.h"
#import "UIControl+Blocks.h"
#import "ServiceNews.h"
#import "UIPlaceHolderTextView.h"
#import "UIViewController+HUD.h"
#import "AccountManager.h"

@interface SendCommentController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UINavigationItem *customNavigationItem;
@property (weak, nonatomic) IBOutlet UIPlaceHolderTextView *commentsTextView;
@end

@implementation SendCommentController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    __weak typeof(self) weak_self = self;
    
    self.commentsTextView.layer.cornerRadius = 5;
    self.commentsTextView.layer.masksToBounds = YES;
    self.commentsTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.commentsTextView.layer.borderWidth = .5f;
    self.commentsTextView.placeholder = @"请输入您的评论内容";
    
    //右侧爆料按钮
    UIButton *buttonComment = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonComment.titleLabel.font = [UIFont systemFontOfSize:18.0f];
    buttonComment.frame = CGRectMake(0, 0, 50.0f, 25.0f);
    [buttonComment setTitle:@"提交" forState:UIControlStateNormal];
    [buttonComment setBackgroundColor:[UIColor redColor]];
    buttonComment.layer.cornerRadius = 10.0f;
    buttonComment.layer.masksToBounds = YES;
    
    [buttonComment addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        
        NSString *comment = weak_self.commentsTextView.text;
        comment = [comment stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
        if(!comment || !comment.length){
            [weak_self showHUDText:@"请输入评论内容!"];
            return;
        }
        
        [ServiceNews doQuestionCommentSend:weak_self.f_wbreplyid
                            content:weak_self.commentsTextView.text
                             userid:[AccountManager sharedAccountManager].f_uid
                             type:weak_self.type
                             commentid:weak_self.commentid
                            success:^(id responseObject, NSString *message) {
                                [weak_self showHUDText:message];
                                //返回
                                [weak_self.navigationController popViewControllerAnimated:YES];
                            } failure:^(NSString *message) {
                                [weak_self showHUDText:message];
                            }];
    }];
    
    UIBarButtonItem *submitItem = [[UIBarButtonItem alloc] initWithCustomView:buttonComment];
    self.customNavigationItem.rightBarButtonItem = submitItem;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (IBAction)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
